package com.b.config;

import com.b.endpoint.ServiceEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig  implements WebSocketConfigurer {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter(); // 自动扫描 @ServerEndpoint 注解[1,3,5](@ref)
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 关键：允许所有来源的跨域连接（开发环境）
        registry.addHandler((WebSocketHandler) new ServiceEndpoint(), "/live")
                .setAllowedOrigins("*"); // 允许所有域名访问
    }
}